package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AlertZoneMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.model.AlertZone;
import zhongbo.java.air.dictionary.service.IAlertZoneService;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AlertZoneServiceImpl implements IAlertZoneService {

    @Autowired
    private AlertZoneMapper alertZoneMapper;


    @Override
    public String addAlertZone(AlertZone alertZone) {
        if (alertZone ==null) {
            throw new RuntimeException("此警戒区域对象为空");
        }

        if (alertZone.getEffectiveEnd().equals("") || alertZone.getEffectiveEnd() == null) {
            log.info("未设置警戒区域生效结束时间，故设置为永久有效");
        }

        alertZone.setCreateTime(new Date());
        int insert = alertZoneMapper.insert(alertZone);
        if (insert > 0) {
            return alertZone.getId();
        }
        throw new RuntimeException("新增警戒区域数据失败");
    }

    @Override
    public AlertZone updateAlertZone(AlertZone alertZone) {
        if (alertZone == null) {
            throw new RuntimeException("此警戒区域对象为空");
        }

        if (alertZone.getEffectiveEnd().equals("") || alertZone.getEffectiveEnd() == null) {
            log.info("未设置警戒区域生效结束时间，故设置为永久有效");
        }

        alertZone.setUpdateTime(new Date());
        int update = alertZoneMapper.updateById(alertZone);
        if (update > 0) {
            return alertZone;
        }
        throw new RuntimeException("修改警戒区域失败");
    }

    @Override
    public int deleteAlertZone(String airId) {
        if (airId == null) {
            throw new RuntimeException("此警戒区域id为空");
        }
        int delete = alertZoneMapper.deleteById(airId);
        if (delete > 0) {
            return delete;
        }
        throw new RuntimeException("删除警戒区域失败");
    }

    @Override
    public AlertZone getAlertZoneById(String airId) {
        if (airId == null) {
            throw new RuntimeException("此警戒区域id为空");
        }
        return alertZoneMapper.selectById(airId);
    }

    @Override
    public List<AlertZone> getaAlertZoneAll() {
        return alertZoneMapper.getAllAlertZone();
    }

    @Override
    public PageVo<AlertZone> page(@RequestParam(required = false) String zoneCode,
                                  @RequestParam(defaultValue = "1") int pageNumber,
                                  @RequestParam(defaultValue = "1") int pageSize) {
        QueryWrapper<AlertZone> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(zoneCode)) {
            queryWrapper.like("zone_code",zoneCode);
        }
        IPage pageParam = new Page(pageNumber,pageSize);
        IPage alertZonePage = alertZoneMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(alertZonePage);
    }

    public boolean isHasTheAddId(String airId) {
        List<AlertZone> allAir = alertZoneMapper.getAllAlertZone();
        return allAir.stream().noneMatch(air -> air.getId().equals(airId));
    }
}
